﻿package com.neriksworkshop.colourLoversDemos.colourExplore
{
	import com.neriksworkshop.colourLoversDemos.colourExplore.data.ColourNeighbourhood;
	import com.neriksworkshop.colourLoversDemos.colourExplore.view.MainView;
	import com.neriksworkshop.lib.colourLovers.items.Color;
	import com.neriksworkshop.lib.colourLovers.items.Palette;
	import com.neriksworkshop.lib.colourLovers.events.RequestEvent;
	import com.neriksworkshop.lib.colourLovers.RequestManager;
	import com.neriksworkshop.lib.colourLovers.requests.params.OrderCol;
	import com.neriksworkshop.lib.colourLovers.requests.PaletteRequest;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class MainController extends Sprite 
	{
		
		private var currentPalette:Palette;
		
		private var colourNeighbourhoods:/*ColourNeighbourhood*/Array = new Array();
		
		private var initialPaletteReqId:int;
		
		private var mainView:MainView;
		private var rm:RequestManager;
		
		public function MainController():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
					
			mainView = new MainView();
			mainView.addEventListener(MouseEvent.CLICK, onViewClick)
			addChild(mainView);
			
			rm = RequestManager.getInstance();
						
			var initialPaletteReq:PaletteRequest = new PaletteRequest();
			initialPaletteReq.orderCol = OrderCol.TOP;
			//initialPaletteReq.resultOffset = Math.floor(Math.random()*20);
			initialPaletteReq.resultOffset = 2;
			initialPaletteReq.numResults = 1;
			initialPaletteReq.addEventListener(RequestEvent.RESULTS, onInitialPaletteLoaded);
			initialPaletteReqId = rm.request(initialPaletteReq);
			
			
		}
		
		private function onViewClick(e:MouseEvent):void 
		{
			if (!(e.target is MovieClip)) return;
			var sq:MovieClip = e.target as MovieClip;
			var paletteObj:Object = colourNeighbourhoods[sq.index].getHighestRankedPalette();
			setNewPalette(paletteObj.palette, sq.index, paletteObj.newPaletteIndex)
			
		}
		
		private function onInitialPaletteLoaded(e:RequestEvent):void 
		{
			setNewPalette((e.target as PaletteRequest).palette);
		}
		
		private function onNeighbourPaletteLoaded(e:RequestEvent):void 
		{
			var neighbourInd:int;
			for (var i:int = 0; i < 5; i++) { if (e.uId == colourNeighbourhoods[i].currentReqId) { neighbourInd = i; break; } }
			
			if (e.type == RequestEvent.RESULTS) colourNeighbourhoods[neighbourInd].addPalettes((e.target as PaletteRequest).palettes, (e.target as PaletteRequest).hex);
			
			mainView.setAvailability(colourNeighbourhoods[neighbourInd].index, colourNeighbourhoods[neighbourInd].nbPalettes);
			
			//trace(neighbourInd, colourNeighbourhoods[neighbourInd].nbPalettes);
			if (colourNeighbourhoods[neighbourInd].nbPalettes < 10) loadNeighbours(neighbourInd);
		}		
		
	
		
		private function setNewPalette(palette:Palette, clickedIndex:int =-1, newPaletteIndex:int =-1):void
		{
			currentPalette = palette;
			
			for (var i:int = 0; i < 5; i++) 
			{
				if (i != clickedIndex)
				{
					colourNeighbourhoods[i] = new ColourNeighbourhood(currentPalette.colorEntities[i], i);
					//colourNeighbourhoods[i].addEventListener("paletteNeighbourhoodReady", onPaletteNeighbourhoodReady)
				}
				
			}
			
			startLoadingNeighbours();
			
			mainView.showPalette(currentPalette, clickedIndex, newPaletteIndex);
			
			
			/*var colourNeigb:ColourNeighbourhood = new ColourNeighbourhood(currentPalette.colorEntities[0]);
			for (var i:int = 0; i < 20; i++) 
			{
				trace(colourNeigb.getNextNeighbourHex());
			}
			
			
			
			
			
			for (var i:int = 0; i < 5; i++) 
			{
				if (i != clickedItem)
				{
					colourNeighbourhoods[i] = new ColourNeighbourhood(currentPalette.colorObjects[i]);
				}
				
			}*/

			
		}
		
		//private function onPaletteNeighbourhoodReady(e:Event):void 
		//{
			//mainView.setAvailability((e.target as ColourNeighbourhood).index, true)
		//}

		private function startLoadingNeighbours():void
		{
			
			rm.stopAll();
			
			for (var i:int = 0; i < 5; i++) 
			{
				loadNeighbours(i);
			}
		}
		
		
		private function loadNeighbours(i:int):void
		{
			var neighbourReq:PaletteRequest = new PaletteRequest();
			neighbourReq.hex = colourNeighbourhoods[i].getNextNeighbourHex();
			neighbourReq.addEventListener(RequestEvent.RESULTS, onNeighbourPaletteLoaded);
			neighbourReq.addEventListener(RequestEvent.NO_RESULTS, onNeighbourPaletteLoaded);
			colourNeighbourhoods[i].currentReqId = rm.request(neighbourReq);
		}
		

		
		
		
		
	}
	
}